﻿using BCData.WeChatComponent.WeChatMessagePushRecord;
using BCData.WeChatComponent.WeChatMessagePushResultVerificationRecord;
using BCData.WeChatComponent.WeChatNewsMaterialArticleRecord;
using BCEntity.WeChatComponent.WeChatMessagePushRecord;
using BCEntity.WeChatComponent.WeChatMessagePushResultVerificationRecord;
using BCEntity.WeChatComponent.WeChatNewsMaterialArticleRecord;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage;
using WeChatSDK.WeChatDto.Conversation.ReceivingMessage.EventMessage;

namespace BCService.WeChatComponent.WeChatHandleService.EventMessageService.MPCallbackEventMessageService
{
    /// <summary>
    /// 微信群发消息回调事件处理
    /// </summary>
    public class MPCallbackEventMessageService : IWeChatMessageHandle
    {
        private readonly IWeChatMessagePushRecordData weChatMessagePushRecordData;
        private readonly IWeChatMessagePushResultVerificationRecordData weChatMessagePushResultVerificationRecordData;
        private readonly IWeChatNewsMaterialArticleRecordData weChatNewsMaterialArticleRecordData;
        public MPCallbackEventMessageService(IWeChatMessagePushRecordData weChatMessagePushRecordData,
            IWeChatMessagePushResultVerificationRecordData weChatMessagePushResultVerificationRecordData,
            IWeChatNewsMaterialArticleRecordData weChatNewsMaterialArticleRecordData)
        {
            this.weChatMessagePushRecordData = weChatMessagePushRecordData;
            this.weChatMessagePushResultVerificationRecordData = weChatMessagePushResultVerificationRecordData;
            this.weChatNewsMaterialArticleRecordData = weChatNewsMaterialArticleRecordData;
        }

        public string HandleMessage(ReceivingMessageDto message)
        {
            MPCallbackEventMessageDto callbackEventMessageDto = message as MPCallbackEventMessageDto;
            WeChatMessagePushRecordEntity pushRecordEntity = this.weChatMessagePushRecordData.GetEntity(callbackEventMessageDto.MessageID);
            pushRecordEntity.MsgId = callbackEventMessageDto.MessageID;
            pushRecordEntity.CheckState = callbackEventMessageDto.CopyrightCheckResult.CheckState;
            pushRecordEntity.MsgStatus = callbackEventMessageDto.Status;
            pushRecordEntity.TotalCount = callbackEventMessageDto.TotalCount;
            pushRecordEntity.FilterCount = callbackEventMessageDto.FilterCount;
            pushRecordEntity.SentCount = callbackEventMessageDto.SentCount;
            pushRecordEntity.ErrorCount = callbackEventMessageDto.ErrorCount;
            this.weChatMessagePushRecordData.UpdateMPStatus(pushRecordEntity);
            IEnumerable<WeChatNewsMaterialArticleRecordEntity> weChatNewsMaterialArticleRecordEntities  = this.weChatNewsMaterialArticleRecordData.Get(pushRecordEntity.MediaId);
            foreach (CopyrightCheckResultsListDto item in callbackEventMessageDto.CopyrightCheckResult.ResultList)
            {
                this.weChatMessagePushResultVerificationRecordData.Add(new WeChatMessagePushResultVerificationRecordEntity()
                {
                    MsgId = pushRecordEntity.MsgId,
                    MediaId = pushRecordEntity.MediaId,
                    WeChatNewsMaterialArticleRecordId = weChatNewsMaterialArticleRecordEntities.FirstOrDefault(p => p.Index == item.ArticleIdx-1).WeChatNewsMaterialArticleRecordId,
                    Index = item.ArticleIdx,
                    UserDeclareState = item.UserDeclareState,
                    AuditState = item.AuditState,
                    OriginalArticleUrl = item.OriginalArticleUrl,
                    OriginalArticleType = item.OriginalArticleType,
                    CanReprint = item.CanReprint,
                    NeedReplaceContent = item.NeedReplaceContent,
                    NeedShowReprintSource = item.NeedShowReprintSource
                });
            }
            return "success";
        }
    }
}
